﻿#region Imports
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Collections.Generic;
using System;
using BulkRenamer.Util;
using System.Drawing;
#endregion

namespace BulkRenamer.App.Controls
{
    /// <summary>
    /// BulkRenamer TextBox control to hold address
    /// </summary>
    public class BRAddressTextBox : BRTextBox
    {
        #region Properties
        public bool IsFileName { get; set; }

        /// <summary>
        /// Tooltip of control
        /// </summary>
        public BRInvalidPathTooltip InvalidPathToolTip { get; set; }
        #endregion

        #region Constructors
        /// <summary>
        /// Default constructor
        /// </summary>
        public BRAddressTextBox()
            : base()
        {
            InvalidPathToolTip = new BRInvalidPathTooltip(this);

            this.Click += new EventHandler(BRAddressTextBox_Click);
            this.KeyDown += new KeyEventHandler(BRAddressTextBox_KeyDown); // new KeyEventHandler(EventList.TextControl_SelectAll);
            this.KeyUp += new KeyEventHandler(BRAddressTextBox_KeyUp);
            this.KeyPress += new KeyPressEventHandler(BRAddressTextBox_KeyPress);
        }
        #endregion

        #region Formatting events
        private void BRAddressTextBox_Click(object sender, EventArgs e)
        {
            string path = this.Text;
            if (Form.ModifierKeys == Keys.Control)
                if (Utilities.ValidatePath(path, IsFileName ? Utilities.ePathValidation.File : Utilities.ePathValidation.Directory))
                    Utilities.OpenPath(path);

            //enable_hyperlink_or_make_visible // RKD1234
        }

        private void BRAddressTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Form.ModifierKeys == Keys.Control) return;

            char chr = e.KeyChar;
            char cOnlyOne = ':';

            List<char> ignoreKeys = new List<char>();
            ignoreKeys.Add('\b');
            ignoreKeys.Add('\r');
            ignoreKeys.Add('\n');
            ignoreKeys.Add('\\');
            if (!IsFileName) ignoreKeys.Add(':');

            List<char> invalidPathChars = Path.GetInvalidPathChars()
                .Union(Path.GetInvalidFileNameChars())
                .Distinct().Where(c => !ignoreKeys.Contains(c)).ToList();

            e.Handled = IsFileName
                ? Path.GetInvalidFileNameChars().Contains(chr)
                : (invalidPathChars.Contains(chr) || (chr == cOnlyOne && this.Text.Contains(cOnlyOne)));

            if (e.Handled) InvalidPathToolTip.ShowToolTip();
            else InvalidPathToolTip.HideToolTip();
        }

        private void BRAddressTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (Form.ModifierKeys == Keys.Control)
            {
                BRAddressTextBox txt = (BRAddressTextBox)sender;
                txt.Font = new Font(txt.Font.FontFamily, txt.Font.Size, FontStyle.Underline, txt.Font.Unit, txt.Font.GdiCharSet);
                txt.Cursor = Cursors.Hand;

                if (e.KeyCode == Keys.A)
                    EventList.TextControl_SelectAll(sender, e); // Ctrl+A
            }
        }

        private void BRAddressTextBox_KeyUp(object sender, KeyEventArgs e)
        {
            BRAddressTextBox txt = (BRAddressTextBox)sender;
            txt.Font = new Font(txt.Font.FontFamily, txt.Font.Size, InitialFontStyle, txt.Font.Unit, txt.Font.GdiCharSet);
            txt.Cursor = InitialCursor;
        }
        #endregion
    }
}
